/*
 * @Author: zy
 * @Date: 2021-10-25 17:20:32
 * @Last Modified by:   lt
 * @Last Modified time: 2021-10-27 15:37:14
 */

import { request } from 'umi';

type CommonResponseType = {
  code: number;
  message: string;
};

// 获取图书列表的查询条件
type QueryParamsType = {
  searchKey?: string;
  typeAid?: number;
  page: number;
  pageSize: number;
};

type BookListItemType = {
  aid: number;
  isbn: string;
  typeName: string;
  name: string;
  quantity: number;
  createTime: string;
};

// 1、获取图书列表
type BookListDataType = {
  code: number;
  message: string;
  data: {
    rowNumber: number;
    totalNumber: number;
    list: BookListItemType[];
  };
};

// 2、获取图书类型
type BookTypesDataType = {
  code: number;
  message: string;
  data: BookTypesItemType[];
};

export type BookTypesItemType = {
  aid: number;
  name: string;
};

// 4、校验损坏数量返回数据
type CheckDamageNumberDataType = {
  code: number;
  message: string;
  data: {
    isExcess: boolean;
  };
};

// 4.1、校验损坏数量参数
type CheckDamageNumberParamsType = {
  aid: number;
  demageQuantity: number;
};

// 5、损坏图书
type DamageBookBodyType = {
  aid: number;
  demageQuantity: number;
  remarks?: string;
};

// 6、新增修改时获取修改图书信息
type OptionBookInfoDataType = {
  code: number;
  message: string;
  data: BookInfoType;
};

export type BookInfoType = {
  name: string;
  typeName: string;
  typeAid: string;
  isbn: string;
  quantity: number;
  remarks: string;
};

export type BookTypeItem = {
  aid: number;
  name: string;
  children: BookTypeItem[] | [];
};
// 7、新增修改时获取图书类型
type OptionBookTypesDataType = {
  code: number;
  message: string;
  data: BookTypeItem[] | [];
};

// 8、新增修改时检验ISBN是否相同
type OptionCheckIsbnDataType = {
  code: number;
  message: string;
  data: {
    isExist: boolean;
    bookInfo: {
      name: string;
      typeAid: number;
      typeName: string;
      isbn: string;
      quantity: number;
      remarks: string;
    };
  };
};

// 9、新增修改时新增/修改图书信息
type OptionSaveBookBodyType = {
  aid: number;
  name: string;
  isbn: string;
  quantity: number;
  remarks?: string;
  typeAid: number;
};

// 1、请求获取图书列表
export async function getBookList(params: QueryParamsType) {
  return request<BookListDataType>(`/bookManage/getBookList`, {
    method: 'GET',
    params,
  });
}

// 2、请求获取图书类型
export async function getBookType() {
  return request<BookTypesDataType>(`/bookManage/getBookType`, {
    method: 'GET',
  });
}

// 3、请求删除图书
export async function delBook(body: { aid: number }) {
  return request<CommonResponseType>(`/bookManage/delBook`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

// 4、请求校验损坏数量
export async function checkDamageNumber(params: CheckDamageNumberParamsType) {
  return request<CheckDamageNumberDataType>(`/bookManage/checkDamageNumber`, {
    method: 'GET',
    params,
  });
}

// 5、请求损坏图书
export async function damageBook(body: DamageBookBodyType) {
  return request<CommonResponseType>(`/bookManage/damageBook`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

// 6、请求新增修改时获取修改图书信息
export async function getBookInfo(params: { aid: number }) {
  return request<OptionBookInfoDataType>(`/bookManage/getBookInfo`, {
    method: 'GET',
    params,
  });
}

// 7、请求新增修改时获取图书类型
export async function getAllType() {
  return request<OptionBookTypesDataType>(`/bookManage/getAllType`, {
    method: 'GET',
  });
}

// 8、请求新增修改时检验ISBN是否相同
export async function checkIsbn(params: { aid: number; isbn: string }) {
  return request<OptionCheckIsbnDataType>(`//bookManage/checkIsbn`, {
    method: 'GET',
    params,
  });
}

// 9、请求新增修改时新增/修改图书信息
export async function saveBook(body: OptionSaveBookBodyType) {
  return request<CommonResponseType>(`/bookManage/saveBook`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}
